<%@ page import="entity.ImageItem" %>
<%@ page import="util.ItemHelper" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.net.URLEncoder"%>
<%!
    public String getNavHref(int offset, int number, String tag, String htmlid)
    {
        if(tag != null){
            return "ladder_table.jsp?htmlid=" + htmlid + "&number=" + number + "&offset=" + offset + "&tag=" + tag;
        }
        else
        { 
            return "ladder_table.jsp?htmlid=" + htmlid + "&number=" + number + "&offset=" + offset;
        }
    }
%>
<% 
    //parse params
    String soffset = request.getParameter("offset");
    String snumber = request.getParameter("number");
    String tag = request.getParameter("tag");
    String htmlid = request.getParameter("htmlid");
    if(htmlid == null)
    {
        htmlid = "laddertable";
    }
    int offset = 0;
    int number = 7;
    if(soffset != null)
    {
        try{
        offset = Integer.parseInt(soffset);
        }
        catch(NumberFormatException e){;}
    }
    if(snumber != null)
    {
        try{
            number = Integer.parseInt(snumber);
            }
            catch(NumberFormatException e){;}
    }

  List<ImageItem> results = ItemHelper.getLadder(offset, number, tag);
//prev-next things
%>

<%-- navigation bar --%>
<div class='laddernav'>
	<input type="submit" <%= (offset > 0)?"":"disabled='disabled'" %> onclick='$("#<%=htmlid %>").load("<%= getNavHref(0, number, tag, htmlid)%>")' value="&lt;&lt;">
	<input type="submit" <%= (offset > 0)?"":"disabled='disabled'" %> onclick='$("#<%=htmlid %>").load("<%= getNavHref(Math.max(offset-number,0), number, tag, htmlid)%>")' value="&lt;">
	<input type="submit" <%= (results.size() == number)?"":"disabled='disabled'" %> onclick='$("#<%=htmlid %>").load("<%= getNavHref(offset+number, number, tag, htmlid)%>")' value="&gt;">
</div>
<% 
  //table of results
if(!results.isEmpty()){
      %>
      <table class=laddertable>
      <%
      int idx = 1 + offset;
      for(ImageItem item: results)
      {%>
          <%-- temporary output for testing --%>
          <tr>
            <td class=ladderank><%= idx %></td>
            <td class='image-holder'><a class=ladderimagelink href="browser.jsp?url=<%= URLEncoder.encode(item.getUrl()) %><%= (tag == "null")?"":("&tag="+tag) %>"><img class='ladderimage' title='<%= item.getTitle() %>' src="<%= item.getUrl() %>"></a></td>
            <td class='ladderrating'><%= item.getRating() %></td>
            <%-- probably should remove... --%>
            <td><%=item.getTags() %></td>
          </tr>
       <%
          idx += 1;
      }%>
      </table>
  <%}%>


